package FirstExam10tasks;

import java.util.Arrays;

import org.omg.CORBA.FREE_MEM;

public class Task8 {

	public static void main(String[] args)
	{
		String list = "kamenitza, zagorka, shumensko, ariana, amstel, burgasko, ledenika, pirinsko";

		putTheWordsInFrame(list);
	}
	
	private static void putTheWordsInFrame(String list)
	{
		
		String[] beers = list.split(", ");
				
		String[] frame = new String [beers.length + 2];
		
		int longestWord = 0;
		
		longestWord = findTheLongestWord(beers, longestWord);

		addWords(beers, frame);

		doUpDownFrame(frame, longestWord);
				
		addSpace(frame, longestWord);

		doTheRightFrame(frame);

		
		System.out.println();
		for (int i = 0; i < frame.length; i++)
		{
			System.out.println(frame[i]);
		}
		
		
		
		
	}
	
	private static String[] doUpDownFrame (String[] frame, int longestWord)
	{
		frame[0] = "*";
		for(int i = 0; i <= longestWord; i++)
		{
			frame[0] += "*";
		}
		
		frame[frame.length - 1] = "*";
		for(int i = 0; i <= longestWord; i++)
		{
			frame[frame.length - 1] += "*";		
		}
		
		return frame;
	}

	private static int findTheLongestWord (String[] beers, int longestWord)
	
	{
		for(int i = 0; i < beers.length; i++)			// namira nai-dylgata duma > longestWord
		{			
			if(beers[i].length() > longestWord)
			{
				longestWord = beers[i].length();
			}
		}
		return longestWord;
	}

	private static String[] addSpace (String[] frame, int longestWord)
	{
		for(int i = 1; i < frame.length - 1; i++) 		//add space
		{			
			for(int j = frame[i].length(); j < longestWord + 1; j++)
			{
				frame[i] += " ";
			}
		}
		return frame;
	}

	private static String[] addWords (String[] beers, String[] frame)
	{
		for(int i = 1; i < beers.length + 1; i++)      // slaga dumite v ramkata + " * "
		{
			frame[i] = "*" + beers[i-1];
		} 
		
		return frame;
	}

	private static String[] doTheRightFrame (String[] frame)
	{
		for(int i = 1; i < frame.length - 1; i++)
		{
			frame[i]  += "*";

		}
		return frame;
	}
}
